#!/usr/bin/env bash
# run work task script on remote server

while getopts :f:ht opt
do case "$opt" in
    f) fl=$OPTARG ;;
	h)  echo
		echo "Usage: sync-run -f work_script -t"
        echo ">>> work_script: script used to run on remote, must be a shell script contains qsub_header."
        echo ">>>              You can use relative/absolute file path on server file system."
        echo ">>> if -t option specified, the command will run as job mode, it usually called by sync-deploy command."
        echo ">>>"
        echo ">>> examples:"
        echo "              sync-run -f work.sh      # this is regular mode, basically you wanna this if you run this script independently"
        echo "         or"
        echo "              sync-run -n work.sh -t   # this is job mode, basically used to be called by sync-deploy command"
        echo "                                       # it will additionally generate a job_id file in the same directory as work_script"
        echo
        exit ;;
    t)  batch="y" ;;
	*) echo "Unknown option: $opt"
		echo
		sync-run -h
        exit ;;
	esac
done

# Get setting info
#source "$(pwd)/syn-setting"
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$DIR/syn-setting"

# if variable port is not set, set 22 as default
if [ ! -n "$port" ]; then
	port=22
fi

if [ ! -n "$batch" ]; then
    batch="n"
fi
# you can also use absolute path
if [ $batch == "y" ]; then
    echo ">>> run as job mode......."
    ssh -p $port $remote_user@$remote_ip "cd $(dirname $fl); qsub $(basename $fl) >> $(dirname $fl)/job_id; echo 'job id is'; cat $(dirname $fl)/job_id; rm $(dirname $fl)/job_id"
else
    echo ">>> run as regular mode......."
    ssh -p $port $remote_user@$remote_ip "qsub $fl"
fi
#ssh -p 22 liuxs@10.15.22.110 "qsub ~/test/work.sh"
